package com.pets.web.service.impl;

import com.github.yulichang.toolkit.JoinWrappers;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.pets.common.utils.OssUtils;
import com.pets.common.utils.StringUtils;
import com.pets.web.entity.PetsComment;
import com.pets.web.entity.PetsGoods;
import com.pets.web.entity.PetsOrders;
import com.pets.web.entity.PetsUser;
import com.pets.web.enums.CommentAuditEnums;
import com.pets.web.enums.OrderCommentStatusEnums;
import com.pets.web.mapper.PetsCommentMapper;
import com.pets.web.service.IPetOrdersService;
import com.pets.web.service.IPetsCommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pets.web.service.IPetsGoodsService;
import com.pets.web.service.IPetsUserService;
import com.pets.web.vo.ApiPetsCommentAddRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 商品评论表 服务实现类
 * </p>
 *
 * @author zhurenwang
 * @since 2024-05-26
 */
@Service
public class PetsCommentServiceImpl extends ServiceImpl<PetsCommentMapper, PetsComment> implements IPetsCommentService {

    @Autowired
    private IPetsUserService petsUserService;
    @Autowired
    private IPetOrdersService petOrdersService;

    @Override
    public int deleteById(Integer id) {
        return baseMapper.deleteById(id);
    }

    @Override
    public List<PetsComment> list(PetsComment petsComment) {
        MPJLambdaWrapper<PetsComment> wrapper = JoinWrappers.lambda(PetsComment.class)
                .selectAll(PetsComment.class)
                .selectAs(PetsGoods::getName, PetsComment::getGoodsName)
                .innerJoin(PetsGoods.class, PetsGoods::getId, PetsComment::getGoodsId)
                .eq(Objects.nonNull(petsComment.getGoodsId()), PetsComment::getGoodsId, petsComment.getGoodsId())
                .like(StringUtils.isNotEmpty(petsComment.getNickname()), PetsComment::getNickname, petsComment.getNickname())
                .like(StringUtils.isNotEmpty(petsComment.getComment()), PetsComment::getComment, petsComment.getComment())
                .eq(Objects.nonNull(petsComment.getAudit()), PetsComment::getAudit, petsComment.getAudit())
                .orderByDesc(PetsComment::getSort).orderByDesc(PetsComment::getCreateTime);
        return baseMapper.selectJoinList(PetsComment.class, wrapper);
    }

//    @Override
//    public int add(PetsComment petsComment) {
//        PetsGoods petsGoods = petsGoodsService.getById(petsComment.getGoodsId());
//        PetsUser petsUser = petsUserService.getById(petsComment.getUserId());
//        petsComment.setNickname(petsUser.getNickname());
//        petsComment.setAvatar(petsUser.getHeadImgUrl());
//        petsComment.setSysUserId(petsGoods.getSysUserId());
//        return baseMapper.insert(petsComment);
//    }

    @Override
    public boolean top(Integer id) {
        int sort = baseMapper.selectMaxSort();
        return lambdaUpdate().set(PetsComment::getSort, ++sort).eq(PetsComment::getId, id).update();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int add(ApiPetsCommentAddRequest request) {
        PetsUser petsUser = petsUserService.getByOpenId(request.getOpenId());
        if (Objects.isNull(petsUser)) {
            throw new IllegalArgumentException("不存在的用户");
        }
        PetsOrders orders = petOrdersService.selectByOrderNo(request.getOrderNo());
        if (Objects.isNull(orders)) {
            throw new IllegalArgumentException("不存在的订单");
        }
        if (!StringUtils.equals(orders.getOpenId(), petsUser.getOpenId())) {
            throw new IllegalArgumentException("订单和用户不匹配订单");
        }
        if (orders.getCommentStatus() == OrderCommentStatusEnums.COMMENTED) {
            throw new IllegalArgumentException("已评论该订单");
        }
        orders.setCommentStatus(OrderCommentStatusEnums.COMMENTED);
        petOrdersService.updateById(orders);
        PetsComment comment = new PetsComment();
        comment.setGoodsId(orders.getGoodsId());
        comment.setOrderNo(orders.getOrderNo());
        comment.setUserId(orders.getUserId());
        comment.setNickname(petsUser.getNickname());
        comment.setAvatar(petsUser.getHeadImgUrl());
        comment.setComment(request.getComment());
        comment.setPics(String.join(",", request.getPics()));
        comment.setScore(request.getScore());
        comment.setSysUserId(orders.getSysUserId());
        return baseMapper.insert(comment);
    }

    @Override
    public List<PetsComment> getCommentList(Integer goodsId) {
        PetsComment petsComment = new PetsComment();
        petsComment.setGoodsId(goodsId);
        petsComment.setAudit(CommentAuditEnums.AUDIT);
        return this.list(petsComment);
    }

    @Override
    public boolean audit(Integer id) {
        return lambdaUpdate()
                .set(PetsComment::getAudit, CommentAuditEnums.AUDIT)
                .eq(PetsComment::getId, id)
                .update();
    }

    @Override
    public int add(PetsComment petsComment) {
        PetsUser petsUser = petsUserService.getById(petsComment.getUserId());
        PetsOrders petsOrders = petOrdersService.selectByUserId(petsComment.getUserId(), petsComment.getGoodsId());
        if (Objects.isNull(petsOrders)) {
            throw new IllegalArgumentException("当前用户不存在已支付订单数据");
        }
        petsComment.setOrderNo(petsOrders.getOrderNo());
        petsComment.setNickname(petsUser.getNickname());
        petsComment.setAvatar(petsUser.getHeadImgUrl());
        petsComment.setAudit(CommentAuditEnums.AUDIT);
        petsComment.setSysUserId(petsOrders.getSysUserId());
        return baseMapper.insert(petsComment);
    }
}
